package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.foundation.domain.entity.SysTenantPackage;
import com.dyna.foundation.domain.form.SysTenantPackageSaveForm;
import com.dyna.foundation.domain.req.SysTenantPackagePageReq;
import com.dyna.foundation.domain.resp.SysTenantPackageResp;
import com.dyna.foundation.domain.resp.SysTenantPackageSimpleResp;
import com.dyna.service.SysTenantPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(value = "租户套餐")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/tenant-package")
public class SysTenantPackageController {

    private final SysTenantPackageService tenantPackageService;

    @PostMapping("/create")
    @ApiOperation(value = "创建租户套餐")
    public R<Long> createTenantPackage(@Valid @RequestBody SysTenantPackageSaveForm form) {
        return R.data(tenantPackageService.createTenantPackage(form));
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新租户套餐")
    public R<Boolean> updateTenantPackage(@Valid @RequestBody SysTenantPackageSaveForm updateReqVO) {
        tenantPackageService.updateTenantPackage(updateReqVO);
        return R.isSuccess(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除租户套餐")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true)})
    public R<Boolean> deleteTenantPackage(@RequestParam("id") Long id) {
        tenantPackageService.deleteTenantPackage(id);
        return R.isSuccess(true);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得租户套餐")
    public R<SysTenantPackageResp> getTenantPackage(@RequestParam("id") Long id) {
        SysTenantPackage tenantPackage = tenantPackageService.getTenantPackage(id);
        return R.data(BeanUtil.toBean(tenantPackage, SysTenantPackageResp.class));
    }

    @GetMapping("/page")
    @ApiOperation(value = "获得租户套餐分页")
    public R<PageResult<SysTenantPackageResp>> getTenantPackagePage(@Valid SysTenantPackagePageReq req) {
        PageResult<SysTenantPackage> pageResult = tenantPackageService.getTenantPackagePage(req);
        return R.data(PageUtil.convertPageResult(pageResult, SysTenantPackageResp.class));
    }

    @ApiOperation(value = "获取租户套餐精简信息列表", notes = "只包含被开启的租户套餐，主要用于前端的下拉选项")
    @GetMapping({"/get-simple-list", "simple-list"})
    public R<List<SysTenantPackageSimpleResp>> getTenantPackageList() {
        List<SysTenantPackage> list = tenantPackageService.getTenantPackageListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return R.data(PageUtil.convertPageResult(list, SysTenantPackageSimpleResp.class));
    }

}
